//
//  GYVisitor.m
//  Visitor
//
//  Created by x on 2017/9/13.
//  Copyright © 2017年 HLB. All rights reserved.
//

#import "GYVisitor.h"
#import "GYNetWorkTools.h"

#import "NSString+FilePath.h"

@implementation GYVisitor


+ (NSMutableArray *)visitors:(NSArray *)array {
    NSMutableArray *visitors = [NSMutableArray array];
    for (NSDictionary *dic in array) {
        GYVisitor *model = [[GYVisitor alloc] initWith:dic];
        [visitors addObject:model];
    }
    return visitors;
}

- (instancetype)initWith:(NSDictionary *)dic {
    self = [super init];
    if (self) {
        [self setValuesForKeysWithDictionary:dic];
    }
    return self;
}

- (void)setResult:(int)result {
    _result = result;
    switch (result) {
        case 0:
            _state = @"未处理";
            _backgroundColor = RGBColor(0XCCCCCC);
            break;
            
        case 1:
            _state = @"同意";
            _backgroundColor = RGBColor(0X2BE97B);
            break;
            
        case 2:
            _state = @"拒绝";
            _backgroundColor = RGBColor(0XFF5C54);
            
        default:
            break;
    }
}
- (void)setValue:(id)value forUndefinedKey:(NSString *)key {

}

+ (void)loadVisitorData:(successCallBack)visitorCallBack {
    
    [GYNetWorkTools loadVisitorData:^(NSDictionary *obj) {
        if ([[obj objectForKey:@"status"] intValue] == 200) {
            NSMutableArray *array = [GYVisitor visitors:[obj objectForKey:@"visitors"]];
            
            //1.加载到访客数据之后我们应该要缓存图片
            dispatch_group_t group = dispatch_group_create();
            for (GYVisitor *model in array) {
                if (model.idImage != nil && ![model.idImage isEqualToString:@""] && ![[SDImageCache sharedImageCache] imageFromDiskCacheForKey:model.idImage]) {
                    dispatch_group_enter(group);
                    [GYNetWorkTools loadVisitorfaceimagebase:model.idImage susscess:^(UIImage *image) {
                        NSLog(@"加载访客头像成功...");
                        dispatch_group_leave(group);
                    } filed:^(NSError *error) {
                        NSLog(@"加载访客头像失败...");
                    }];
                }
                
                if (model.faceimagebase != nil && ![model.faceimagebase isEqualToString:@""] && ![[SDImageCache sharedImageCache] imageFromDiskCacheForKey:model.faceimagebase]) {
                    dispatch_group_enter(group);
                    [GYNetWorkTools loadVisitorfaceimagebase:model.faceimagebase susscess:^(UIImage *image) {
                        NSLog(@"加载访客头像成功...");
                        dispatch_group_leave(group);
                    } filed:^(NSError *error) {
                        NSLog(@"加载访客头像失败...");
                    }];
                }
            }
            dispatch_group_notify(group, dispatch_get_main_queue(), ^{
                //当图片缓存完的时候,通知界面加载
                visitorCallBack(array);
            });
        }else if([[obj objectForKey:@"status"] intValue] == 201) {
            //代表key失效,要重新登入
            //1.删除个人信息
            NSError *error;
            [[NSFileManager defaultManager] removeItemAtPath:[NSString CachesDir:@"userInfo.plist"] error:&error];
            if (error == nil) {
                NSLog(@"删除信息没错误...");
                [[NSNotificationCenter defaultCenter] postNotificationName:@"loginOutSuccess" object:nil];
            }
        }
    } filed:^(NSError *error) {
        NSLog(@"loadVisitorData======%@",error);
    }];
}

- (NSString *)description {
    NSArray *keys = @[@"content",@"idImage",@"faceimagebase",@"name",@"phone",@"visitingTime",@"result",@"visitorId",@"cause"];
    
    return [NSString stringWithFormat:@"%@",[self dictionaryWithValuesForKeys:keys]];
}
@end
